import Vue from 'vue';
import Vuex from 'vuex';
const header = { // header
  state: {
    left: '',
    right: '',
    middle: '',
    ifshow: true,
    leftAction: '',
    leftIcon: '',
    rightAction: '/',
    rightIcon: ''
  },
  mutations: {
    changeHeader: function (state, opt) { // this为实例化的store
      console.log('header');
      console.log(this.state);
      this.state.header = opt;
    }
  }
};

const pop = { // 弹出层
  state: {
    ifshow: false,
    showLoading: false,
    showTx: false,
    content: '',
    showConfirm: false, // 提示确认
    confirmTxt: '', // 提示文字
    callback: null,
    showMsg: false,
    msgTitle: '',
    msgContent: '',
    msgBtn: '',
    msgCB: null
  },
  mutations: {
    showLoading: function () { // 显示loading
      this.state.pop = {
        ifshow: true,
        showLoading: true,
        showTx: false,
        content: '',
        showConfirm: false, // 提示确认
        confirmTxt: '',
        callbak: null,
        showMsg: false,
        msgTitle: '',
        msgContent: '',
        msgBtn: '',
        msgCB: null
      };
    },
    closePop: function () { // 关闭pop
      this.state.pop = {
        ifshow: false,
        showLoading: false,
        showTx: false,
        content: '',
        showConfirm: false, // 提示确认
        confirmTxt: '',
        callback: null,
        showMsg: false,
        msgTitle: '',
        msgContent: '',
        msgBtn: '',
        msgCB: null
      };
    },
    doShowTx: function (state, txt) {
      this.state.pop = {
        ifshow: true,
        showLoading: false,
        showTx: true,
        content: txt,
        showConfirm: false, // 提示确认
        confirmTxt: '',
        callbak: null,
        showMsg: false,
        msgTitle: '',
        msgContent: '',
        msgBtn: '',
        msgCB: null
      };
    },
    setConfirm: function (state, obj = {
      ifShow: false,
      text: ''
    }) { // obj{ ifShow , text , callback}
      let pop = this.state.pop;
      pop.ifshow = obj.ifShow;
      pop.showConfirm = obj.ifShow; // 提示确认
      pop.confirmTxt = obj.text;
    },
    setConfirmCallBak: function (state, callback = null) {
      this.state.pop.callback = callback;
    },
    setMsg: function (state, obj = { ifshow: false, showMsg: false, msgTitle: '', msgContent: '', msgBtn: '' }) {
      let pop = this.state.pop;
      pop.ifshow = obj.ifshow;
      pop.showMsg = obj.showMsg;
      pop.msgTitle = obj.msgTitle;
      pop.msgContent = obj.msgContent;
      pop.msgBtn = obj.msgBtn;
    },
    setMsgCb: function (state, callback = null) {
      console.log('callback' + typeof callback);
      this.state.pop.msgCB = callback;
    }
  },
  actions: {
    showTx: function (state, opt) {
      // console.log('state');
      // console.log(state);
      // console.log('opt');
      console.log(opt);
      let time = opt.time || 2000;
      let self = this;
      this.commit('doShowTx', opt.text);
      let timmer = setTimeout(function () {
        self.commit('closePop');
        if (opt.callback) {
          opt.callback();
        }
        clearTimeout(timmer);
        timmer = null;
      }, time);
    }
  }
};

const loginData = { // 登录后的数据
  state: {
    isLogin: window.localStorage.getItem('isLogin') || false,
    userInfo: window.localStorage.getItem('loginData') ? JSON.parse(window.localStorage.getItem('loginData')) : {},
    phone: '',
    pwd: '',
    // successPath:'',
    // logOutPath:''
    userIcon: window.localStorage.getItem('userIcon') || ''
  },
  mutations: {
    setLogin: function (state, opt) {
      let me = this.state.loginData;
      // let goto = me.logOutPath;
      console.log('这是我的 is' + me);
      me.userInfo = opt;
      me.isLogin = true;
      this.commit('setUserIcon', opt.userIcon || '');
      // console.log('goto:' + goto)
      // if (goto) {
      //   this.$router.push(goto);
      //   goto = '';
      // } else {
      //   this.$router.push('-1');
      // }
    },
    clearLogin: function () {
      let me = this.state.loginData;
      // let goto = me.logOutPath;
      me.userInfo = {};
      me.isLogin = false;
      // me.userIcon = '';
      this.commit('setUserIcon', '');
      // if (goto) {
      //   this.$router.push(goto);
      //   goto = '';
      // } else {
      //   this.$router.push('-1');
      // }
    },
    setUserIcon: function (state, str) {
      this.state.loginData.userIcon = str;
    },
    setSuccessPath: function (state, path) {
      this.state.loginData.successPath = path;
    },
    setLogOutPath: function (state, path) {
      this.state.loginData.logOutPath = path;
    }
  }
  // computed: mapState(['isLogin', 'userInfo'])
}

const forgetPd = {
  state: {
    yzm: '',
    // pd: '',
    phone: '',
    optType: ''
  },
  mutations: {
    changePhone: function (state, str) {
      this.state.forgetPd.phone = str;
      // console.log(this.state.forgetPd.phone);
    },
    // changePd:function(state, str) {
    //   this.state.forgetPd.pd = str;
    // },
    changeYzm: function (state, str) {
      this.state.forgetPd.yzm = str;
      // console.log(this.state.forgetPd.yzm);
    },
    changeOpyType: function (state, str) {
      this.state.forgetPd.optType = str;
      // console.log(this.state.forgetPd.yzm);
    },
    clearPh: function () {
      this.state.forgetPd.phone = '';
    },
    clearYzm: function () {
      this.state.forgetPd.yzm = '';
    },
    clearBoth: function () {
      this.commit('clearYzm');
      this.commit('clearPh');
    }
  }
}
const userModify = {
  state: {
    ifFresh: true,
    userInfo: {},
    modify: ''
    // changeData: {}
  },
  mutations: {
    setFresh: function (state, type) {
      this.state.userModify.ifFresh = type;
    },
    setModify: function (state, num) {
      this.state.userModify.modify = num;
    },
    setUserInfo: function (state, obj) {
      this.state.userModify.userInfo = obj;
    },
    // setChangeData: function (state, obj) {
    //   this.state.userModify.changeData = obj;
    // },
    changProp: function (state, arr) { // [{name:'abc', value:'123'}]
      console.log(arr);
      for (let i = 0; i < arr.length; i++) {
        let item = arr[i];
        let name = item.name;
        let value = item.value;
        if (name && value) {
          this.state.userModify.userInfo[name] = value;
        }
      }
    }
  }
}

const passenger = {
  state: {
    person: {},
    passengerPage: 1
  },
  mutations: {
    setPassenger: function (state, opt) {
      let me = this.state.passenger;
      me.person = opt;
    },
    clearPassenger: function () {
      let me = this.state.passenger;
      me.person = {};
    },
    setpassengerPage: function (state, opt) {
      let me = this.state.passenger;
      me.passengerPage = opt;
    }
  }
}
const helpCenter = {
  state: {
    columnId: '',
    detail: '',
    listTitle: '',
    detailTitle: '',
    newsTypeId: '',
    firstDate: [],
    secondDate: []
  },
  mutations: {
    sethelpCenter: function (state, opt) {
      let me = this.state.helpCenter;
      me.columnId = opt.columnId;
      me.detail = opt.detail;
      me.listTitle = opt.listTitle; // 列表题目
      me.detailTitle = opt.detailTitle; // 详情题目
      me.newsTypeId = opt.newsTypeId; // 详情类型 4外链
      me.firstDate = opt.firstDate; // 第一次数据
      me.secondDate = opt.secondDate; // 第二层数据
    },
    clearhelpCenter: function () {
      let me = this.state.helpCenter;
      me.columnId = '';
      me.detail = '';
      me.listTitle = '';
      me.detailTitle = '';
      me.newsTypeId = '';
      me.firstDate = [];
      me.secondDate = [];
    }
  }
}
const newsInfo = {
  state: {
    newsId: '',
    newsTitle: ''
  },
  mutations: {
    setnewsInfo: function (state, opt) {
      let me = this.state.newsInfo;
      me.newsId = opt.newsId;
      me.newsTitle = opt.newsTitle;
    },
    clearnewsInfo: function () {
      let me = this.state.newsInfo;
      me.newsId = '';
      me.newsTitle = '';
    }
  }
}
const bannerInfo = {
  state: {
    newsId: '',
    newsTitle: '',
    newsTypeId: ''
  },
  mutations: {
    setBanner: function (state, opt) {
      let me = this.state.bannerInfo;
      me.newsContent = opt.newsContent;
      me.newsTitle = opt.newsTitle;
      me.newsTypeId = opt.newsTypeId;
    }
  }
}
const allOrders = {
  state: {
    searchType: 0, // 请求的类型，0：全部 1：待支付 2：已支付 3：已出票 4：已取消
    rows: 10, // 统一设置订单一次请求的条数
    allOrdersList: {
      page: 1,
      arr: []
    },
    nopayList: {
      page: 1,
      arr: []
    },
    payList: {
      page: 1,
      arr: []
    },
    finishList: {
      page: 1,
      arr: []
    },
    cancelList: {
      page: 1,
      arr: []
    },
    showOrder: { // 点击列表后，需要展示详情的order
      orderType: '',
      orderId: '',
      orderNo: ''
    }
  },
  mutations: {
    setSearchType: function (state, type) {
      this.state.allOrders.searchType = type;
    },
    addPage: function (state, obj) { // obj: {name:'xxx' , data: data}
      let name = obj.name;
      let data = obj.data;
      let self = this;
      if (name && data) {
        this.state.allOrders[name].page++;
        data.forEach(function (value) {
          self.state.allOrders[name].arr.push(value);
        });
      }
    },
    resetPage: function (state, name) { // name:allOrdersList,nopayList,payList,finishList,cancelList
      let obj = this.state.allOrders[name];
      if (obj) {
        obj.page = 1;
        obj.arr = [];
      } else {
        console.log('没有这个属性');
      }
    },
    freshPage: function (state, obj) { // 刷新专用设置
      let name = obj.name;
      // let data = obj.data;
      this.commit('resetPage', name);
      this.commit('addPage', obj);
    },
    incrementPage: function (state, name) {
      this.state.allOrders[name].page++;
    },
    reductionPage: function (state, name) {
      this.state.allOrders[name].page--;
    },
    showOrder: function (state, obj = { orderType: '', orderId: '', orderNo: '' }) {
      let orderId = obj.orderId;
      let orderType = obj.orderType;
      let orderNo = obj.orderNo;
      this.state.allOrders.showOrder.orderType = orderType;
      this.state.allOrders.showOrder.orderId = orderId;
      this.state.allOrders.showOrder.orderNo = orderNo;
    },
    resetAllOders: function () {
      this.commit('resetPage', 'allOrdersList');
      this.commit('resetPage', 'nopayList');
      this.commit('resetPage', 'payList');
      this.commit('resetPage', 'finishList');
      this.commit('resetPage', 'cancelList');
      this.commit('showOrder');
    }
  }
};

const flights = {
  state: {
    searchOpt: '',
    passengerNum: {
      adt: '1',
      cnn: '0',
      inf: '0'
    }
  },
  mutations: {
    setSearchOpt: function (state, obj) {
      this.state.flights.searchOpt = obj;
    },
    setPassengerNum: function (state, obj) {
      let pN = this.state.flights.passengerNum;
      pN.adt = obj.adt;
      pN.cnn = obj.cnn;
      pN.inf = obj.inf;
    }
  }
}

const airTicketOrder = {
  state: {
    passengers: ''
  },
  mutations: {
    setAirTicketOrder: function (state, obj) {
      this.state.airTicketOrder.passengers = obj;
    }
  }
}

const calendar = {
  state: {
    year: '',
    month: '',
    date: '',
    str: '',
    hasValue: false
  },
  mutations: {
    setSelDay: function (state, obj) {
      let year = obj.year;
      let month = obj.month;
      let date = obj.date;
      let sel = this.state.calendar;
      sel.year = year;
      sel.month = month;
      sel.date = date;
      sel.str = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date);
      sel.hasValue = true;
    },
    initSelDay: function () { // 初始化选中日
      let nextDay = new Date().getTime() + 24 * 60 * 60 * 1000;
      let next = new Date(nextDay);
      let year = next.getFullYear();
      let month = next.getMonth() + 1;
      let date = next.getDate();
      this.commit('setSelDay', {
        year,
        month,
        date
      });
    }
  }
}

const cityPicker = { // 选择城市
  state: {
    type: 0,
    beginCity: '',
    beginCityName: '',
    endCity: '',
    endCityName: '',
    citys: '',
    hotCity: '',
    ifInit: false, // 是否请求过数据
    keys: ''
  },
  mutations: {
    setCity: function (state, obj) { // type:0 begin,1 end; city:'三字码' ;cityName:''
      let city = obj.city;
      let cityName = obj.cityName;
      let cityPicker = this.state.cityPicker;
      let type = obj.type || this.state.cityPicker.type;
      if (type === 0) {
        cityPicker.beginCity = city;
        cityPicker.beginCityName = cityName;
      } else {
        cityPicker.endCity = city;
        cityPicker.endCityName = cityName;
      }
    },
    setCityPickerType: function (state, type) {
      this.state.cityPicker.type = type;
    },
    setCityPickerArr: function (state, arr) { // arr: {name:'', arr:''}
      let self = this;
      // if (arr.name) {}
      arr.forEach(function (item, index, arr) {
        self.state.cityPicker[item.name] = item.arr;
      })
      this.state.cityPicker.ifInit = true;
    }
  }
}

const addValue = {
  state: {
    ifInit: false,
    seledpiId: '',
    baggage: [], // 行李运费和规则
    meal: [], // 餐食
    passengers: [], // 乘客
    seledBag: {},
    seledMeal: [{}, {}, {}]
  },
  mutations: {
    setBaggage: function (state, obj) {
      this.state.addValue.baggage = [];
      let self = this;
      obj.forEach(function (item) {
        self.state.addValue.baggage.push(item);
      })
    },
    setMeal: function (state, obj) {
      this.state.addValue.meal = [];
      let self = this;
      obj.forEach(function (item) {
        self.state.addValue.meal.push(item);
      })
    },
    setAddPassenger: function (state, obj) {
      this.state.addValue.passengers = [];
      console.log('passengers>>>>>>>>>>>>>>>>');
      console.log(obj)
      let self = this;
      obj.forEach(function (item) {
        self.state.addValue.passengers.push(item);
      })
    },
    setSeledBag: function (state, obj) { // obj:{} piId:'123', index: 1
      this.state.addValue.seledBag[obj.piId] = obj.index;
    },
    setSeledpiId: function (state, piId) {
      this.state.addValue.seledpiId = piId;
    },
    setSeledMeal: function (state, obj) { // piId:'123' index 第几类餐食 value 第几种餐食
      console.log(obj);
      let hasSel = this.state.addValue.seledMeal[obj.index]; // 获得第index种餐食的对象
      console.log(hasSel)
      if (hasSel[obj.piId] === obj.value) {
        console.log('in')
        hasSel[obj.piId] = 'cannel';
        this.state.addValue.seledMeal.splice(obj.index, 1, hasSel);
      } else {
        hasSel[obj.piId] = obj.value;
        this.state.addValue.seledMeal.splice(obj.index, 1, hasSel);
      }
    },
    setIfInit: function (state, obj = true) {
      this.state.addValue.ifInit = obj;
    }
  }
}

const confirmOrder = {
  state: {
    showDetails: {},
    auxiliarygo: []
    // airTicketOrder: {}
  },
  mutations: {
    setShowDetails: function (state, obj) {
      this.state.confirmOrder.showDetails = obj;
    },
    setAuxiliarygo: function (state, obj) {
      this.state.confirmOrder.auxiliarygo = obj;
    }
    // setAirTicketOrder: function(state, obj) {
    //   this.state.airTicketOrder.auxiliarygo = obj;
    // }
  }
};
const orderPayment = {
  state: {
    details: {}
  },
  mutations: {
    setOrderPayment: function (state, obj) {
      console.log(obj)
      this.state.orderPayment.details = obj;
    }
  }
};
// s是否是国内国际
const ishome = {
  state: {
    data: {}
  },
  mutations: {
    setIshome: function (state, obj) {
      console.log(obj)
      this.state.ishome.data = obj;
    }
  }
};
// 判断是否是从上一个页面进入
const isAirTicketOrder = {
  state: {
    data: ''
  },
  mutations: {
    setIsAirTicketOrder: function (state, obj) {
      this.state.isAirTicketOrder.data = obj;
    }
  }
}
// 航班关注页面
const airDynamic = {
  state: {
    data: ''
  },
  mutations: {
    setAirDynamic: function (state, obj) {
      this.state.airDynamic.data = obj;
    }
  }
}
// 航班详情
const airDynamicFindjg = {
  state: {
    details: {},
    airNum: {},
    airXX: {},
    airDynamicSel: 0
  },
  mutations: {
    setAirDynamicFindjg: function (state, obj) {
      console.log(obj)
      this.state.airDynamicFindjg.details = obj;
    },
    setAirNum: function (state, obj) {
      console.log(obj)
      this.state.airDynamicFindjg.airNum = obj;
    },
    setAirXX: function (state, obj) {
      console.log(obj)
      this.state.airDynamicFindjg.airXX = obj;
    },
    setAirDynamicSel: function (state, obj) {
      console.log(obj)
      this.state.airDynamicFindjg.airDynamicSel = obj;
    }
  }
};
// 单独购买辅营
const xingli = {
  state: {
    data: {}
  },
  mutations: {
    setXingli: function (state, obj) {
      this.state.xingli.data = obj;
    }
  }
}
const refundApply = {
  state: {
    content: ''
  },
  mutations: {
    setRefundContent: function (state, obj) {
      this.state.refundApply.content = obj;
    }
  }
}

const opt = { // 注入总store
  modules: {
    refundApply,
    header,
    pop,
    loginData,
    forgetPd,
    userModify,
    passenger,
    helpCenter,
    newsInfo,
    flights,
    calendar,
    cityPicker,
    addValue,
    allOrders,
    airTicketOrder,
    confirmOrder,
    orderPayment,
    isAirTicketOrder,
    airDynamic,
    bannerInfo,
    airDynamicFindjg,
    xingli,
    ishome
  }
};
Vue.use(Vuex);
let store = new Vuex.Store(opt);
export default store;
